<?php
	namespace AppBundle\Controller;

	use Symfony\Bundle\FrameworkBundle\Controller\Controller;
	use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
	use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
	use Symfony\Component\HttpFoundation\Request;
	use Symfony\Component\HttpFoundation\Response;
	use AppBundle\Entity\Student;
	use AppBundle\Entity\Major;
	use Symfony\Component\Form\Extension\Core\Type\TextType;
	use Symfony\Component\Form\Extension\Core\Type\DateType;
	use Symfony\Component\Form\Extension\Core\Type\SubmitType;
	use Symfony\Component\Fomr\Extension\Core\Type\IntegerType;
	use AppBundle\Form\Type\StudentType;
	use AppBundle\Form\Type\MajorType;

	class IndexController extends Controller{
		/**
		 * @Route("/index/",name="index")
		 * @Security("has_role('ROLE_USER')")
		 * @param  Request
		 * @return [type]
		 */
		public function indexAction(Request $request){
			$em = $this->getDoctrine()->getManager();
			$student = new Student;
			$repository = $this->getDoctrine()->getRepository("AppBundle:Student");
			$student = $repository->findOneById(1);
			// $major1 = new Major;
			// $major1->setName('fr');
			// $student->addMajor($major1);
		    $form = $this->createForm(StudentType::class, $student);
		    $form->handleRequest($request);
			if($form->isSubmitted() and $form->isValid()){
				$majors = $student->getMajors();
				foreach($majors as $m){
					$m->setStudent($student);
				}
				$em->persist($student);
				$em->flush();
				return new Response("submit success");
			}
			$arr = array(
				'name' => 'xiakai',
				'age'  => 27,
				'wife' => 'liuyu'
			);
			return $this->render('index/index.html.twig', array('form'=>$form->createView(), 'arr' => $arr));
		}

		/**
		 * @Route("/detail/", name="detail")
		 */
		public function detailAction(Request $request){
			// $major = new Major();
			// $major->setName('市场营销');
			// $major->setStatus('1');
			// $s1 = new Student();
			// $s1->setName('刘雨--沉迷于阴阳师 天天刷怪');
			// $s1->setAge(26);
			// $s1->setAddtime(new \Datetime('today'));
			// $major->getStudent()->add($s1);

			// $form = $this->createForm(MajorType::class, $major);

			// $form->handleRequest($request);

			// if($form->isSubmitted() and $form->isValid()){
			// 	$response = new Response("detail submit success");
			// 	return $response;
			// }

			// return $this->render('index/index.html.twig', array('form'=>$form->createView()));
		}
	}
